home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr11.lha / clx / excldep.c < prev    next >
C/C++ Source or Header  |  1990-04-30  |  2KB  |  74 lines

  1. /*
  2.  * Allegro CL dependent C helper routines for CLX
  3.  */
  4.  
  5. /*
  6.  * This code requires select and interval timers.
  7.  * This means you probably need BSD, or a version
  8.  * of Unix with select and interval timers added.
  9.  */
  10.  
  11. #include <sys/types.h>
  12. #include <sys/errno.h>
  13. #include <sys/time.h>
  14. #include <stdio.h>
  15.  
  16. #define ERROR -1
  17. #define INTERRUPT -2
  18. #define TIMEOUT 0
  19. #define SUCCESS 1
  20.  
  21. #ifdef FD_SETSIZE
  22. #define NUMBER_OF_FDS FD_SETSIZE    /* Highest possible file descriptor */
  23. #else
  24. #define NUMBER_OF_FDS 32
  25. #endif
  26.  
  27. /* Length of array needed to hold all file descriptor bits */
  28. #define CHECKLEN ((NUMBER_OF_FDS+8*sizeof(int)-1) / (8 * sizeof(int)))
  29.  
  30. extern int errno;
  31.  
  32. /*
  33.  * This function waits for input to become available on 'fd'.  If timeout is
  34.  * 0, wait forever.  Otherwise wait 'timeout' seconds.  If input becomes
  35.  * available before the timer expires, return SUCCESS.  If the timer expires
  36.  * return TIMEOUT.  If an error occurs, return ERROR.  If an interrupt occurs
  37.  * while waiting, return INTERRUPT.
  38.  */
  39. int fd_wait_for_input(fd, timeout)
  40.     register int fd;
  41.     register int timeout;
  42. {
  43.     struct timeval timer;
  44.     register int i;
  45.     int checkfds[CHECKLEN];
  46.  
  47.     if (fd < 0 || fd >= NUMBER_OF_FDS) {
  48.     fprintf(stderr, "Bad file descriptor argument: %d to fd_wait_for_input\n", fd);
  49.     fflush(stderr);
  50.     }
  51.  
  52.     for (i = 0; i < CHECKLEN; i++)
  53.       checkfds[i] = 0;
  54.     checkfds[fd / (8 * sizeof(int))] |= 1 << (fd % (8 * sizeof(int)));
  55.  
  56.     if (timeout) {
  57.     timer.tv_sec = timeout;
  58.     timer.tv_usec = 0;
  59.     i = select(32, checkfds, (int *)0, (int *)0, &timer);
  60.     } else
  61.       i = select(32, checkfds, (int *)0, (int *)0, (struct timeval *)0);
  62.  
  63.     if (i < 0)
  64.       /* error condition */
  65.       if (errno == EINTR)
  66.     return (INTERRUPT);
  67.       else
  68.     return (ERROR);
  69.     else if (i == 0)
  70.       return (TIMEOUT);
  71.     else
  72.       return (SUCCESS);
  73. }
  74.